package com.vitas.coin.vm

import android.annotation.SuppressLint
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.snap.base.util.BaseUtil
import com.snap.util.startAct
import com.vitas.coin.R
import com.vitas.coin.constant.AppConstant
import com.vitas.coin.data.bean.ChildrenVoiceBean
import com.vitas.coin.data.event.RequestFinishEvent
import com.vitas.coin.manager.PlayManager
import com.vitas.coin.resp.CollectDaoResp
import com.vitas.coin.resp.HomeResp
import com.vitas.coin.ui.act.MusicAct
import com.vitas.coin.ui.act.UploadAct
import com.vitas.coin.ui.adapter.MusicAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HomeVM : BaseMusicVM() {


    val musicName = MutableLiveData("--")

    val homeResp = HomeResp()

    private var cateName :String = "热门铃声"

    private val collectDaoResp by lazy { CollectDaoResp }

    val bannerList = arrayListOf(
        R.mipmap.banner1,R.mipmap.banner2
    )

    val musicList: ArrayList<ChildrenVoiceBean> = ArrayList()

    fun freshListByPlay(musicId: Int, isPlaying:Boolean) {
        for (i in musicList.indices) {
            if (musicList[i].id == musicId){
                musicList[i].isPlaying = isPlaying
            }else{
                if (musicList[i].isPlaying) {
                    musicList[i].isPlaying = false
                }
            }
        }
    }

    fun collect(musicObj: ChildrenVoiceBean) {
        viewModelScope.launch(Dispatchers.IO) {
            if (musicObj.isCollect) {
                musicObj.isCollect = false
                collectDaoResp.delete(musicObj)
            }else{
                musicObj.isCollect = true
                collectDaoResp.upsert(musicObj)
            }

        }
    }





    fun freshListByCollect(musicObj: ChildrenVoiceBean) {
        for (i in musicList.indices) {
            if (musicList[i].id == musicObj.id){
                musicList[i].isCollect = musicObj.isCollect
                musicAdapter.notifyItemChanged(i,AppConstant.UPDATE_COLLECT_STATUS)
            }
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    fun loadData(parentId: Long, pageNum: Int = 1) {
        viewModelScope.launch {
            val dataBaseList = withContext(Dispatchers.IO) {
                collectDaoResp.queryAll()
            }
            homeResp.requestVoiceList(parentId, pageNum, 10) {
                BaseUtil.postEvent(RequestFinishEvent())
                if (it != null) {
                    if (1 == pageNum) {
                        musicList.clear()
                    }
                    musicList.addAll(it)
                    for (tempObj in musicList) {
                        tempObj.parentName = cateName
                        if (tempObj.id == PlayManager.getInstance().currentMusicId
                            &&PlayManager.getInstance().isPlaying){
                            tempObj.isPlaying = true
                            break
                        }
                    }

                    for (i in musicList.indices) {
                        val mItemDataA: ChildrenVoiceBean = musicList[i];
                        for (j in dataBaseList.indices) {
                            val mItemDataB: ChildrenVoiceBean = dataBaseList[j]
                            if (mItemDataA.id == mItemDataB.id){
                                mItemDataA.isCollect = mItemDataB.isCollect
                            }
                        }
                    }
                    musicAdapter.notifyDataSetChanged()
                }

            }
        }
    }


    fun onClickAddUpload() {
        startAct(UploadAct::class.java){
            putExtra(UploadAct.PARENT_ID,0L)
        }
    }

    fun onClickStartOrPause() {
        PlayManager.getInstance().onClickPlayButton()
    }


    fun onClickMore() {
        startAct(MusicAct::class.java){
            putExtra(MusicAct.PARENT_ID,45L)
            putExtra(MusicAct.INTENT_NAME, "热门铃声")
        }
    }

    fun setCateName(cateName:String) {
        this.cateName = cateName
    }

    val musicAdapter by lazy { MusicAdapter(actionAct.invoke(), musicList) }



}